package com.yodawnla.roboAttack.scene;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.entity.modifier.AlphaModifier;
import org.anddev.andengine.entity.modifier.DelayModifier;
import org.anddev.andengine.entity.modifier.LoopEntityModifier;
import org.anddev.andengine.entity.modifier.ScaleModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.sprite.Sprite;

import android.view.KeyEvent;

import com.yodawnla.lib.YoScene;
import com.yodawnla.lib.util.widget.YoButton;
import com.yodawnla.lib.util.widget.YoTiledButton;
import com.yodawnla.roboAttack.system.ME;
import com.yodawnla.roboAttack.system.SE;
import com.yodawnla.roboAttack.system.Tex;
import com.yodawnla.roboAttack.system.YS;

public class TitleScene extends YoScene
{
	@Override
	public void loadScene()
	{
		Sprite bg = new Sprite(0, 0, 480, 800, getTexture(Tex.tilteBg));
		this.attachChild(bg);
		YoButton btn = new YoButton(this, 150, 501, getTexture(Tex.titleBtn))
		{
			@Override
			public void onClickedEvent()
			{
				this.registerEntityModifier(new SequenceEntityModifier(new ScaleModifier(0.05f, 1, 0.8f), new ScaleModifier(0.05f, 0.8f, 1)));
				playSound(SE.Start);
				toScene(YS.StartScene);
			}
		};
		this.attachChild(btn);
		btn.setPosition(240 - btn.getWidth() / 2, 501);

		Sprite sprite = new Sprite(0, 0, getTexture(Tex.titleBtn));
		btn.attachChild(sprite);
		sprite.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
		sprite.registerEntityModifier(new LoopEntityModifier(new SequenceEntityModifier(new AlphaModifier(0.3f, 1, 0), new DelayModifier(0.4f))));
		sprite.registerEntityModifier(new LoopEntityModifier(new SequenceEntityModifier(new ScaleModifier(0.3f, 1, 1.2f), new DelayModifier(0.4f))));


		YoButton fbBtn = new YoButton(this, 6, 600, getTexture(Tex.fbBtn))
		{
			@Override
			public void onPressedEvent()
			{
				playSound(SE.Click);
				showLink("http://www.facebook.com/pages/YoDawnLa-%E9%81%8A%E6%A8%82%E9%BB%A8/473502306006935?ref=stream");
			}
		};
		this.attachChild(fbBtn);

		YoButton rateBtn = new YoButton(this, 77, 600, getTexture(Tex.rateBtn))
		{
			@Override
			public void onPressedEvent()
			{
				playSound(SE.Click);
				showMarketLink();
			}
		};
		this.attachChild(rateBtn);

		YoTiledButton audioBtn = new YoTiledButton(this, 410, 600, getTiledTexture(Tex.Tiled.AudioBtn))
		{
			boolean audioOn;

			@Override
			public void init()
			{
				audioOn = isAudioEnabled();

				if (audioOn)
					this.setCurrentTileIndex(0);
				else
					this.setCurrentTileIndex(1);
			}

			@Override
			public void onClickedEvent()
			{
				this.registerEntityModifier(new SequenceEntityModifier(new ScaleModifier(0.05f, 1, 0.8f), new ScaleModifier(0.05f, 0.8f, 1)));
				audioOn = !audioOn;

				setAudioEnabled(audioOn);
				if (audioOn)
					this.setCurrentTileIndex(0);
				else
					this.setCurrentTileIndex(1);

				playSound(SE.Click);
			}
		};
		this.attachChild(audioBtn);

	}

	@Override
	public void onEnterScene()
	{
		playMusic(ME.BGM);
	}

	@Override
	public void onExitScene()
	{

	}

	@Override
	public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent)
	{
		switch (pKeyCode)
		{
		case KeyEvent.KEYCODE_BACK:
			if (pEvent.getAction() == KeyEvent.ACTION_DOWN)
			{
				return false;
			}
			break;
		case KeyEvent.KEYCODE_MENU:
			if (pEvent.getAction() == KeyEvent.ACTION_DOWN)
			{
				return true;
			}
			break;
		}
		return false;
	}
}